/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pucit;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author Hany
 */
public class carDao {

    private Connection con;

    private void connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1/rentacar";
        con = DriverManager.getConnection(url, "root", "root");
    }

    public boolean addcar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String carname = request.getParameter("carname").toString();
        String model = request.getParameter("model").toString();
        String numberplate = request.getParameter("numberplate").toString();
        String rent = request.getParameter("rent").toString();

        try {
            connect();
            String query = "insert into car_data (carname, model, numberplate, rent, reservedcar) values (?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, carname);
            ps.setString(2, model);
            ps.setString(3, numberplate);
            ps.setString(4, rent);
            ps.setString(5, "0");

            int result = ps.executeUpdate();
            if (result > 0) {
                return true;
            } else {
                return false;
            }



        } catch (Exception e) {
            return false;
        } finally {
            con.close();
        }
    }
    
    
    public ArrayList<Hashtable<String, String>> getAvailableCars() throws Exception{
        String query = "Select * from car_data where reservedcar=\'0\'";
        connect();
        
        PreparedStatement ps = con.prepareStatement(query);
        ResultSet rs = ps.executeQuery();
        
        
        ArrayList<Hashtable<String, String>> cars = new ArrayList<Hashtable<String, String>>();
        
        while(rs.next()){
            Hashtable<String, String> car = new Hashtable<String, String>();
            car.put("carname", rs.getString("carname"));
            car.put("model", rs.getString("model"));
            car.put("rent", "" + rs.getInt("rent"));
            car.put("numberplate", rs.getString("numberplate"));
            car.put("numberplate", rs.getString("numberplate"));
            car.put("id", "" + rs.getInt("id"));
            cars.add(car);
        }
        
        return cars;
    }
    
    public ArrayList<Hashtable<String, String>> getReservedCars() throws Exception{
        String query = "Select * from car_data where reservedcar<>\'0\'";
        connect();
        
        PreparedStatement ps = con.prepareStatement(query);
        ResultSet rs = ps.executeQuery();
        
        
        ArrayList<Hashtable<String, String>> cars = new ArrayList<Hashtable<String, String>>();
        
        while(rs.next()){
            Hashtable<String, String> car = new Hashtable<String, String>();
            car.put("carname", rs.getString("carname"));
            car.put("model", rs.getString("model"));
            car.put("rent", "" + rs.getInt("rent"));
            car.put("numberplate", rs.getString("numberplate"));
            car.put("numberplate", rs.getString("numberplate"));
            car.put("id", "" + rs.getInt("id"));
            cars.add(car);
        }
        
        return cars;
    }
    
    public boolean reserveCar(HttpServletRequest request, HttpServletResponse response)throws Exception{
        connect();
        String username = request.getSession().getAttribute("username").toString();
        String id = request.getParameter("carId").toString();
        
        String query = "Update car_data set reservedcar=\'"+username+"\' where id="+id+";";
        Statement st = con.createStatement();
        int result = st.executeUpdate(query);
        
        if(result != -1)
            return true;
        else
            return false;
        
    }
    
    public boolean freeCar(HttpServletRequest request, HttpServletResponse response)throws Exception{
        connect();
        String id = request.getParameter("carId").toString();
        
        String query = "Update car_data set reservedcar=\'0\' where id="+id+";";
        Statement st = con.createStatement();
        int result = st.executeUpdate(query);
        
        if(result != -1)
            return true;
        else
            return false;
        
    }
    
    public boolean deleteCar(HttpServletRequest request, HttpServletResponse response)throws Exception{
        connect();
        String id = request.getParameter("carId").toString();
        
        String query = "Delete from car_data where id="+id+";";
        Statement st = con.createStatement();
        int result = st.executeUpdate(query);
        
        if(result != -1)
            return true;
        else
            return false;
        
    }
}
